<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<head>
<jsp:include page="/views/include.jsp"></jsp:include>
<script type="text/javascript">

    $.parser.onComplete = function() {
		$('body').css('visibility','visible');
		setTimeout(function() {
			$('#loading-mask').remove();
		},50);
    };
    
    
    $(function() {
    	$('#dt_dataSorce').combobox({
			url:"<c:url value='/etl/mapping_dataSource.tg'/>",
		    panelHeight:'auto',
		    required:'true',
		    valueField:'dataSource_id',
		    textField:'dataSource_name',
		    onSelect:function(rec){
		    	$('#dt-table').datagrid('load',{
		    		dataSourceId: rec.dataSource_id
		    	});	
		    },
		    onLoadSuccess:function(data){
		    	if(data.length == 1){
		    		var dataSourceId = data[0].dataSource_id;
		    		$('#dt-table').datagrid('load',{
			    		dataSourceId: dataSourceId
			    	});
		    		$('#dt_dataSorce').combobox('setValue', dataSourceId);
		    	}
		    }
		});
    	
    	
    	$("#dt-table").datagrid({
			onDblClickRow:function(rowIndex, rowData){
				$('#table_update_form')[0].reset();
				$('#table_update_form').formid('loadit',rowData);
				$('#table_update_dialog').dialog('setTitle','修改表信息').dialog('open');	
			},
			onClickRow:function(rowIndex, rowData){
				if(rowData.display){
					$("#handle2 span span").html("移除");
				}else{
					$("#handle2 span span").html("新增");
				}
			}
			
		});
	});
    
    
    
    
    function statusFormatter(value){
		if(value){
			return "是";
		}else{
			return "否";
		}
	}
    
    function existsFormatter(value) {
		if(value) {
			return "存在";
		} else {
			return "丢失";
		}
	}
    
    
    function updateItem() {
		$('#table_update_form').form('submit', {
			url:'<c:url value="/etl/table_update.tg"/>',
			onSubmit:function() { return true; },
			dataType:"json",
			success:function(result) {
				var data = eval("(" + result + ")");
				if(data.success) {
					$.messager.show({
						title		:	'提示ʾ',
						msg			:	data.message,
						showType	:	'slide'
					});
					$('#table_update_dialog').dialog('close');
					$('#dt-table').datagrid('loadData', {total:0,rows:[]});
					$('#dt-table').datagrid("reload");
				} else {
					$.messager.alert('操作失败!','error');
				}
			}
		});
	}
    
    
    function searchItem() {
    	if($("#keyword").val()){
    		var queryParams = $('#dt-table').datagrid('options').queryParams;
    		queryParams.key = $("#keyword").val();
    		$('#dt-table').datagrid('loadData', {total:0,rows:[]});
			$('#dt-table').datagrid("reload");
    	}else{
    		var queryParams = $('#dt-table').datagrid('options').queryParams;
    		queryParams.key = "";
    		$('#dt-table').datagrid('loadData', {total:0,rows:[]});
			$('#dt-table').datagrid("reload");
    	}
	}
    
    function searchItem2() {
    	var queryParams = $('#dt-table').datagrid('options').queryParams;
    	queryParams.key = "";
    	$('#dt-table').datagrid('loadData', {total:0,rows:[]});
		$('#dt-table').datagrid("reload");
	}
    
    function updateItem2(){
    	var row = $('#dt-table').datagrid('getSelected');
		if (row) {
			$('#table_update_form')[0].reset();
			$('#table_update_form').formid('loadit',row);
			$('#table_update_dialog').dialog('setTitle','修改表信息').dialog('open');	
		} else {
			$.messager.show({
				title:'注意',
				msg:'请先选择数据，再进行修改。'
			});
		}
    }
    
    
    function refreshTable(){
		var row = $('#dt-table').datagrid('getSelected');
		if (row) {
			if (confirm("确定要重新加载数据源吗?")) {
				$.ajax({
					typpe			: "post",
					url 			: '<c:url value="/etl/table_reloadProperty.tg"/>',
					dataType		: 'json',
					data			: {"tableId" : row["table_id"] },
					success			: function(result) {
						var data = eval(result);
						if (data.success) {
							$.messager.show({
								title		:	'提示',
								msg			:	"操作成功",
								showType	:	'slide'
							});
							refresh();
						} else {
							$.messager.alert('警告', data.message, 'error');
						}
					}
				});
			}
		} else {
			$.messager.show({
				title:'注意',
				msg:'请先选择数据，再进行修改。'
			});
		}
	}
    
    function editItem() {
		var row = $('#dt-table').datagrid('getSelected');
		if (row) {
			if (confirm("确定 管理/取消管理 表吗 ?")) {
				$.ajax({
					typpe			: "post",
					url 			: '<c:url value="/etl/table_edit.tg"/>',
					dataType		: 'json',
					data			: {"table_id" : row["table_id"] },
					success			: function(result) {
						var data = eval(result);
						if (data.success) {
							$.messager.show({
								title		:	'提示',
								msg			:	data.message,
								showType	:	'slide'
							});
							$('#dt-table').datagrid('loadData', {total:0,rows:[]});
							$('#dt-table').datagrid('reload');
						} else {
							$.messager.alert('警告','操作失败！','error');
						}
					}
				});
			}
		} else {
			$.messager.show({
				title:'注意',
				msg:'请先选择数据，再进行修改。'
			});
		}
	}
</script>
</head>
<body style="margin: 0; padding: 0; height: 100%; overflow: hidden; background: #F2FBFF">
	<div id="mainlayout" class="easyui-layout" fit="true" >
		<div region="north" border="false">
			<div class="toolbar">
				<table cellpadding="0" cellspacing="0" style="width: 95%;">
					<tr>
						<td>
							数据源: <input id="dt_dataSorce" class="easyui-combobox" style="width:200px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        					<input id="keyword" class="easyui-validatebox">
        					<a href="javascript:searchItem()" class="easyui-linkbutton" iconCls="icon-search" plain="true" >搜索</a>
        					<a id="handle1" href="javascript:searchItem2()" class="easyui-linkbutton" iconCls="icon-search" plain="true" >查询已配置表</a>
        					<a id="handle2" href="javascript:editItem()" class="easyui-linkbutton" iconCls="icon-edit" plain="true" >新增</a>
        					<a id="handle3" href="javascript:updateItem2()" class="easyui-linkbutton" iconCls="icon-edit" plain="true" >修改</a>
        					<a href="javascript:refreshTable()" class="easyui-linkbutton" iconCls="icon-reload" plain="true">重新加载表</a> 
						</td>
					</tr>
				</table>
			</div>
		</div>
		<div region="center" border="false" >
			<table id="dt-table" class="easyui-datagrid" url="<c:url value='/etl/table_data.tg'/>" fit="true" pageSize=20 "border="false" pagination="true" striped="true" singleSelect="true" rownumbers="true">
				<thead>
				 	<th field="table_id" width="100">编号</th>
					<th field="table_name" width="250">数据表名称</th>
					<th field="table_alais" width="250">数据表中文名</th>
					<th field="table_exists" formatter="existsFormatter" width="250">数据表是否存在</th>
					<th field="display" formatter="statusFormatter" width="250">是否管理</th>
				</thead>
			</table>
		</div>
	</div>
	
	<!-- 修改 Table Dialog start -->
		<div id="table_update_dialog" class="easyui-dialog" closed="true" modal="true" buttons="#table_update_dialog-buttons">
			<form id="table_update_form" method="post">
				<input type="hidden" id="table_id" name="table_id">
				<div style="padding:20px">
					<table cellpadding="0" cellspacing="0" class="form-table">
						<tr>
							<td>数据表名称</td>
							<td><input id="table_name" class="easyui-validatebox" style="width:250px" disabled="true" /></td>
						</tr>
						<tr>
							<td>数据表中文名</td>
							<td><input id="table_alais" name="table_alais" class="easyui-validatebox" style="width:250px" /></td>
						</tr>
					</table>
				</div>
			</form>
			<div id="table_update_dialog-buttons" style="text-align: right;">
				<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#table_update_dialog').dialog('close')">取消</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-save" onclick="updateItem()">更新</a> 
			</div>
		</div>
		<!-- 修改 Table Dialog end -->
</body>